package cn.eshore.bnet.bpm.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Result;

import cn.eshore.bnet.bpm.dto.ext.Customer;

public class JAXBUtil {
    public static void xml2java() throws JAXBException {
        JAXBContext j = JAXBContext.newInstance("cn.eshore.bnet.bpm.dto");
        Unmarshaller unmarshaller = j.createUnmarshaller();
        File f = new File("Customer.xml");
        Object object = unmarshaller.unmarshal(f);
        System.out.println(object.getClass().getName());
    }

    public static String java2xml(Object object) throws JAXBException, UnsupportedEncodingException {
        JAXBContext j = JAXBContext.newInstance("cn.eshore.bnet.bpm.dto");
        Marshaller marshaller = j.createMarshaller();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        marshaller.marshal(object, byteArrayOutputStream);
        String xml = new String(byteArrayOutputStream.toByteArray(),"UTF-8");
        return xml;
    }
}
